package DControl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DCursor extends DComponent implements Runnable{
	int index = 0;
	Color XORcolor = Color.white;
	Thread thread;
	boolean flag = false;
	boolean turn = true;
	DTextField tfd;
	
	public DCursor(DTextField tfd)
	{
		this.tfd = tfd;
		init();
	}
	
	public DCursor(int x,int y,DTextField tfd)
	{
		init();
	    this.x = x;
	    this.y = y;
	    this.tfd = tfd;
	    
	}
	
	public DCursor(int x,int y,Color color,Color XORcolor)
	{
		init();
		this.x = x;
		this.y = y;
		this.color = color;
		this.XORcolor = XORcolor;
		
	}
	
	public void init()
	{
		
		x = 0;
		y = 0;
		color = Color.black;
		thread = new Thread(this);
		thread.start();
	}
	
	public void run()
	{
	    while(turn)
	    {
	    	try
			{
				Thread.sleep(500);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
				
		    //tfd.repaint();
		    flag = !flag;
		    //System.out.println(index);
			
	    }
	
	}
	
	public void moveNext(int distance)
	{
		this.x += distance;
	}
	
	public void movePrev(int distance)
	{
		this.x -= distance;
	}
	
	public void paintComponent(Graphics g)
	{
			if(!flag)
			{
				g.setColor(color);
			
			}
			else
			{
			
				g.setColor(XORcolor);
			}
			//g.setColor(color);
			FontMetrics fm = g.getFontMetrics();
			g.fillRect(x, y, 2, fm.getHeight());
	}

	public void setIndex(int index)
	{
		this.index = index;
	}
	
	public void setXORColor (Color XORcolor)
	{
		this.XORcolor = XORcolor;
	}
	
}
